local getmetatable = getmetatable
local setmetatable = setmetatable
local module = module
local require = require
local print = print
local type = type
local error = error
local string = string
local pcall = pcall
local tonumber = tonumber

module('eg.music.Music')
_VERSION='0.1'

local function track(self)
    return self.trackNumber .. " of " .. self.trackCount
end

local function time(self)
    -- return Math.round(self.seconds / 0.6) / 100.0
    return string.format("%.2f", (self.seconds / 0.6) / 100)
end

local function tokenizeString(s)
    tokens = {}
    i = 1
    while true do
        tab = string.find(s, "\t", i)
        if tab == nil then break end
        sub = string.sub(s, i, tab-1)
        i = tab+1
        tokens[#tokens+1] = sub
    end
    tokens[#tokens+1] = string.sub(s,i,#s)

    return tokens
end

function __call(self, string)
    music = {}

    tokens = tokenizeString(string)
    music.title = tokens[1]
    music.artist = tokens[2]
    music.album  = tokens[3]
    music.genre = tokens[4]
    music.size = tonumber(tokens[5])
    music.seconds = tonumber(tokens[6])
    music.trackNumber = tonumber(tokens[7])
    music.trackCount = tonumber(tokens[8])
    music.year = tonumber(tokens[9])
    music.date = tokens[10]

    music.time = time
    music.track = track

    return music
end

setmetatable(_M, _M)
